import {
    Controller,
    Get,
    Post,
    Body, Session,
    UseGuards,
    Redirect,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiOkResponse, getSchemaPath } from '@nestjs/swagger';

import { ResponseJson } from '@/common/type/app.dto';
import { ResponseCode } from '@/common/type/enum';
import { LoginService } from './login.service';
import { LoginDto } from './dto/login.dto';
import { UpdatePwDto } from '@/sys/dto/create-sy.dto';

import { SysMenu } from '@/sys/entities/sys.entity';
import { UpdateSysMenuDto } from '@/sys/dto/update-sy.dto';
import { AuthGuard } from '@/common/guard/auth.guard';



@Controller('login')
@ApiTags('登录')

export class LoginController {
    constructor(private readonly loginService: LoginService) { }

    @Get('captcha')
    @ApiOperation({ summary: '生成验证码', description: '生成验证码' })
    @ApiOkResponse({
        description: '返回svg格式图片',
        schema: {
            allOf: [
                { $ref: getSchemaPath(ResponseJson) },
                {
                    properties: {
                        obj: {
                            type: 'string',
                            description: 'SVG图片'
                        }
                    }
                }
            ]

        },
        // type: ResponseJson
    })
    createCaptcha(@Session() session: Record<string, any>): ResponseJson<string, null> {
        const captcha = this.loginService.createCaptcha();
        if (captcha) {
            session['CAPTCHA'] = captcha.text;
        }
        const r: ResponseJson<string, any> = {
            code: ResponseCode.SuccessNoMsg,
            obj: captcha.data
        }
        return r;
    }

    /**
     * 借用一下，限制凯镖系统使用, 开启路由可以使用，关闭禁止使用
     * @returns 
     */
    @Get('KB')
    checkKB() {
        return 'Y';
    }


    @Post('login')
    @ApiOperation({ summary: '登录'})
    @ApiOkResponse({
        type: ResponseJson
    })
    async login(
        @Body() loginDto: LoginDto,
        @Session() session: Record<string, any>,
    ) {
        const r = await this.loginService.login(loginDto, session);
        return r;
    }

    @Post('logoff')
    @UseGuards(AuthGuard)
    @ApiOperation({ summary: '退出' })
    @ApiOkResponse({
        type: ResponseJson
    })
    async logoff(@Session() session: Record<string, any>) {
        return this.loginService.logoff(session);
    }

    @Post('updatepw')
    @UseGuards(AuthGuard)
    @ApiOperation({ summary: '修改密码' })
    @ApiOkResponse({
        type: ResponseJson
    })
    updatePw(
        @Body() updatePw: UpdatePwDto,
        @Session() session: Record<string, any>,
    ) {
        const r = this.loginService.updatePw(updatePw, session['user']);
        // console.log(session['user']['pass']);
        return r;
    }

    @Post('getmenutreebyuser')
    @UseGuards(AuthGuard)
    @ApiOperation({ summary: '取个人菜单tree' })
    @ApiOkResponse({
        schema: {
            allOf: [
                { $ref: getSchemaPath(ResponseJson) },
                {
                    properties: {
                        obj: {
                            type: 'array',
                            description: '菜单',
                            items: {
                                $ref: getSchemaPath(UpdateSysMenuDto)
                            }
                        }
                    }
                }
            ]
        },
    })
    async getUserMenuTree(@Session() session: Record<string, any>): Promise<ResponseJson<SysMenu[], null>> {
        const userId = session['user']['id'];
        return this.loginService.getUserMenuTree(userId);
    }
}
